package com.example.qingyiapp.ui.activity.fragment.fragment_FindUI.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.qingyiapp.R;
import com.example.qingyiapp.service.entify.FindWaitYouBean;
import com.example.qingyiapp.ui.activity.fragment.fragment_FindUI.bean.QuestionDA;

import java.net.URL;
import java.util.ArrayList;

/**
 * Creadted by Met·Wang on 2019/9/29 14:41
 * Describe: NullPointerException Write
 */
public class QuestionListViewAdapter extends BaseAdapter {

    Html.ImageGetter imgGetter = new Html.ImageGetter() {
        public Drawable getDrawable(String source) {
            Drawable drawable = null;
            URL url = null;
            try {
                url = new URL(source);
                drawable = Drawable.createFromStream(url.openStream(), "img");
            } catch (Exception e) {
                return null;
            }
            drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable
                    .getIntrinsicHeight());
            return drawable;
        }
    };


    ArrayList<QuestionDA.DataBean> list;
    Context context;

    public QuestionListViewAdapter(ArrayList<QuestionDA.DataBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {


        ViewHolder viewHolder=null;

        if(convertView==null){

            viewHolder=new ViewHolder();

            convertView=View.inflate(context,R.layout.daan_item,null);

            viewHolder.icon=convertView.findViewById(R.id.find_square_waityou_item_Icon);

            viewHolder.content2=convertView.findViewById(R.id.find_square_waityou_item_content2);
            viewHolder.userName=convertView.findViewById(R.id.find_square_waityou_item_userName);
            viewHolder.time=convertView.findViewById(R.id.find_square_waityou_item_time);


            convertView.setTag(viewHolder);



        }else {
            viewHolder= (ViewHolder) convertView.getTag();
        }

        QuestionDA.DataBean dataBean = list.get(position);

        Glide.with(context).load(dataBean.getUser().getUsercover()+"").apply(RequestOptions.circleCropTransform()).into(viewHolder.icon);
        viewHolder.userName.setText(dataBean.getUser().getName()+"");

        String time = dataBean.getTime();
        String[] s = time.split(" ");
        viewHolder.time.setText(s[0]+"");

        viewHolder.content2.setText(list.get(position).getSummary()+"");


        return convertView;
    }

    class ViewHolder{
        ImageView icon;
        TextView userName,content2,time;
    }


}
